/**
 * Created by zhanglihe on 15/10/30.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog'
], function ($, common, ready, template, Collection, Model, dialog) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },
        fetch: function (_args) {
            var _this = this;
            var args = _args || {};
            args.limit = 10;
            args.offset = 0;
            args.id = id;

            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'agency_order_detail_list',
                condition: args,
                model: {
                    tag_name: 'tr',
                    event: {},
                    change: function (e) {
                        this.data[$(e.target).attr('name')] = $(e.target).val();
                    }
                },
                callback: function () {
                    loaded();
                    $('.pagination').prepend('<li><a href="javascript:">结果合计：￥' + _this.collection.result._amount + '</a></li>');
                    $('.pagination').prepend('<li><a href="javascript:">本页合计：￥' + _this.collection.result._page_amount + '</a></li>');
                }
            });
            this.collection.fetch();
        },
        event: function () {
            var _this = this;
            /**
             * 搜索事件
             */
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                var sn = $('input[name="sn"]').val();
                _this.collection.condition.sn = sn;
                _this.collection.reload();
            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }

    };

    new index();

});